package file;
import java.util.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

import java.awt.*;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import javax.swing.*;

import java.awt.Desktop;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class HandleListener implements ActionListener {
	JTextArea inputText, showText;
	private Object File;

	public void setInputText(JTextArea text) {
		inputText = text;
	}

	public void setShowText(JTextArea text) {
		showText = text;
	}

	public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals("copy"))
			showText.copy();
		else if (str.equals("cut"))
			showText.cut();
		else if (str.equals("paste"))
			inputText.paste();
		else if (str.equals("save")) {		
		String text = showText.getText();
		File fWrite = new File("F:\\english.txt");
		try {
			Writer out = new FileWriter(fWrite);
			BufferedWriter bufferedWriter = new BufferedWriter(out);
			bufferedWriter.write(inputText.getText());
			bufferedWriter.newLine();
			bufferedWriter.close();
		} catch(IOException e1) {
		    e1.printStackTrace();
		}	
		}
		else if (str.equals("open")){
			JFileChooser fileChooser;
			fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			fileChooser.showDialog(fileChooser, null);
			File = fileChooser.getSelectedFile();
			String filepath = ((java.io.File) File).getAbsoluteFile().toString();
			showText.setText(null);
			try {
				BufferedReader bufferedReader =new BufferedReader(new FileReader(filepath));
				String line;
				while((line=bufferedReader.readLine())!=null) {
					showText.append(line+"\n");
				}
				bufferedReader.close();
			}catch(IOException e1){
				e1.printStackTrace();
			}
		}
		else if(str.equals("replace")) {
			String oldString = JOptionPane.showInputDialog("请输入替换的字符");
			String newString = JOptionPane.showInputDialog("请输入新的字符");
			showText.setText(showText.getText().replace(oldString, newString));
		}

			
	}
}
	
	
	/*public void save() {
		String text = showText.getText();
		File fWrite = new File("F:\\english.txt");
		try {
			Writer out = new FileWriter(fWrite);
			BufferedWriter bufferedWriter = new BufferedWriter(out);
			bufferedWriter.write(inputText.getText());
			bufferedWriter.newLine();
			bufferedWriter.close();
		} catch(IOException e1) {
		    e1.printStackTrace();
		}
		
	}
	
	
	public void open() {
		JFileChooser fileChooser;
		fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fileChooser.showDialog(fileChooser, null);
		File = fileChooser.getSelectedFile();
		String filepath = ((java.io.File) File).getAbsoluteFile().toString();
		showText.setText(null);
		try {
			BufferedReader bufferedReader =new BufferedReader(new FileReader(filepath));
			String line;
			while((line=bufferedReader.readLine())!=null) {
				showText.append(line+"\n");
			}
			bufferedReader.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	
}*/



